<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/12/14 9:24
 * Desc: 景点人群年龄规则配置
 */
class Model_Spot_People_Config extends ORM
{

    protected $_table_name = 'spot_people_config';

    public static function del_by_group_id($people_group_id)
    {
        DB::delete('spot_people_config')->where('people_group_id', '=', $people_group_id)->execute();
    }

    /**
     * 保存景点人群配置
     * @param $spot_id
     * @param $params
     * @return void
     * @throws Kohana_Exception
     */
    public static function save_spot_people_config($spot_id, $params)
    {
        $people_group     = Arr::get($params, 'people_group', '');
        $people_group_ids = array();
        if ($people_group && is_array($people_group)) {
            $people_age_type = Arr::get($params, 'people_age_type');
            $start_age       = Arr::get($params, 'start_age');
            $start_age_type  = Arr::get($params, 'start_age_type');
            $end_age         = Arr::get($params, 'end_age');
            $end_age_type    = Arr::get($params, 'end_age_type');
            $age_type        = Arr::get($params, 'age_type');

            foreach ($people_group as $people_group_id) {
                if (isset($people_age_type[$people_group_id])) {
                    // 更新数据
                    $orm = ORM::factory('spot_people_config')
                        ->where('spot_id', '=', $spot_id)
                        ->where('people_group_id', '=', $people_group_id)
                        ->find();
                    if (empty($orm->id)) {
                        $orm->addtime = time();
                    }
                    $orm->spot_id         = $spot_id;
                    $orm->people_group_id = $people_group_id;
                    $orm->start_age       = $start_age[$people_group_id];
                    $orm->start_age_type  = $start_age_type[$people_group_id];
                    $orm->age_type        = $age_type[$people_group_id];
                    $orm->type            = $people_age_type[$people_group_id];
                    if ($people_age_type[$people_group_id] == 2) {
                        $orm->end_age      = $end_age[$people_group_id];
                        $orm->end_age_type = $end_age_type[$people_group_id];
                    }
                    $orm->modtime = time();
                    $orm->save();

                    $people_group_ids[] = $people_group_id;
                }
            }
        }

        // 删除不存在的人群
        $builder = DB::delete('spot_people_config')
            ->where('spot_id', '=', $spot_id);
        if (!empty($people_group_ids)) {
            $builder->where('people_group_id', 'not in', $people_group_ids);
        }
        $builder->execute();

        // 删除不存在的人群规则配置
        $builder = DB::delete('spot_ticket_people_rule')
            ->where('spot_id', '=', $spot_id);
        if (!empty($people_group_ids)) {
            $builder->where('people_group_id', 'not in', $people_group_ids);
        }
        $builder->execute();
    }

    /**
     * 获取景点人群配置
     * @param $spot_id
     * @return array
     */
    public static function get_spot_edit_config($spot_id)
    {
       $configs = DB::select('b.name', array('b.id','group_id'), 'a.*')
            ->from(array('spot_people_config', 'a'))
            ->join(array('spot_people_group', 'b'), 'right')
            ->on('b.id', '=', 'a.people_group_id')
            ->on('a.spot_id', '=', DB::expr($spot_id))
            ->where('b.is_show', '=', 1)
            ->group_by('b.id')
            ->execute()
            ->as_array();

       foreach ($configs as &$item) {
           $item['des'] = '';
           if($item['id']) {
               // 处理人群描述
               $item['des'] = self::make_config_des($item);
           }
       }

       return $configs;
    }

    /**
     * 生成人群描述
     * @param $item
     * @param int $show_age_type 是否显现年龄计算方式，默认 1 显示；
     * @return string
     */
    public static function make_config_des($item, $show_age_type = 1) {
        $des = $item['start_age']. '周岁';
        $des .= $item['start_age_type'] == 1 ? '（含）' : '（不含）';
        if($item['type'] == 2){
            $des .= '- '. $item['end_age']. '周岁';
            $des .= $item['end_age_type'] == 1 ? '（含）' : '（不含）';
        }else{
            $des .= '以上';
        }
        if($show_age_type == 1) {
            $des .= $item['age_type'] == 1 ? '（按生日计算年龄）' : '（按年份计算年龄）';
        }
        return $des;
    }
}